/**
 * @file test_all.cpp
 * @brief 综合测试运行器
 * 运行所有测试用例并生成详细报告
 */

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <chrono>

class ComprehensiveTestRunner {
public:
    struct TestSuite {
        std::string name;
        std::string executable;
        std::string description;
    };
    
    ComprehensiveTestRunner() {
        // 定义所有测试套件
        test_suites_ = {
            {
                "基本功能测试",
                "./test_moparse",
                "测试基本的Verilog解析、保存、加载和反向生成功能"
            },
            {
                "Verilog语法测试", 
                "./test_verilog_syntax",
                "测试各种Verilog语法结构的解析能力"
            },
            {
                "边界情况测试",
                "./test_edge_cases", 
                "测试边界条件、错误处理和异常情况"
            },
            {
                "反向生成测试",
                "./test_reverse_generation",
                "测试所有反向生成模式和各种模块类型"
            },
            {
                "文件列表测试",
                "./test_filelist_features",
                "测试文件列表功能、嵌套结构和错误处理"
            }
        };
    }
    
    int run_all_tests() {
        std::cout << "==================================================" << std::endl;
        std::cout << "         Moparse 综合测试套件" << std::endl;
        std::cout << "==================================================" << std::endl;
        std::cout << std::endl;
        
        auto start_time = std::chrono::high_resolution_clock::now();
        
        int total_suites = 0;
        int passed_suites = 0;
        int failed_suites = 0;
        
        for (const auto& suite : test_suites_) {
            total_suites++;
            
            std::cout << "--------------------------------------------------" << std::endl;
            std::cout << "运行测试套件: " << suite.name << std::endl;
            std::cout << "描述: " << suite.description << std::endl;
            std::cout << "可执行文件: " << suite.executable << std::endl;
            std::cout << "--------------------------------------------------" << std::endl;
            
            auto suite_start = std::chrono::high_resolution_clock::now();
            
            int result = std::system(suite.executable.c_str());
            
            auto suite_end = std::chrono::high_resolution_clock::now();
            auto suite_duration = std::chrono::duration_cast<std::chrono::milliseconds>(
                suite_end - suite_start);
            
            if (result == 0) {
                std::cout << "✅ " << suite.name << " - 通过 (耗时: " 
                         << suite_duration.count() << "ms)" << std::endl;
                passed_suites++;
            } else {
                std::cout << "❌ " << suite.name << " - 失败 (耗时: " 
                         << suite_duration.count() << "ms)" << std::endl;
                failed_suites++;
            }
            
            std::cout << std::endl;
        }
        
        auto end_time = std::chrono::high_resolution_clock::now();
        auto total_duration = std::chrono::duration_cast<std::chrono::milliseconds>(
            end_time - start_time);
        
        // 输出总结报告
        print_summary_report(total_suites, passed_suites, failed_suites, total_duration.count());
        
        return failed_suites > 0 ? 1 : 0;
    }

private:
    void print_summary_report(int total, int passed, int failed, long duration_ms) {
        std::cout << "==================================================" << std::endl;
        std::cout << "                 测试总结报告" << std::endl;
        std::cout << "==================================================" << std::endl;
        std::cout << std::endl;
        
        std::cout << "📊 测试统计:" << std::endl;
        std::cout << "   总测试套件数: " << total << std::endl;
        std::cout << "   通过套件数:   " << passed << std::endl;
        std::cout << "   失败套件数:   " << failed << std::endl;
        std::cout << "   成功率:       " << (passed * 100.0 / total) << "%" << std::endl;
        std::cout << "   总耗时:       " << duration_ms << "ms" << std::endl;
        std::cout << std::endl;
        
        std::cout << "🎯 测试覆盖范围:" << std::endl;
        std::cout << "   ✓ 基本Verilog解析功能" << std::endl;
        std::cout << "   ✓ 参数化模块支持" << std::endl;
        std::cout << "   ✓ 复杂端口声明" << std::endl;
        std::cout << "   ✓ 宏定义和条件编译" << std::endl;
        std::cout << "   ✓ 模块实例化" << std::endl;
        std::cout << "   ✓ 存储器和数组" << std::endl;
        std::cout << "   ✓ 系统函数支持" << std::endl;
        std::cout << "   ✓ 生成块语法" << std::endl;
        std::cout << "   ✓ 任务和函数" << std::endl;
        std::cout << "   ✓ 文件列表解析" << std::endl;
        std::cout << "   ✓ 嵌套文件列表" << std::endl;
        std::cout << "   ✓ 循环引用检测" << std::endl;
        std::cout << "   ✓ 反向代码生成" << std::endl;
        std::cout << "   ✓ 边界条件处理" << std::endl;
        std::cout << "   ✓ 错误恢复机制" << std::endl;
        std::cout << std::endl;
        
        std::cout << "🔧 反向生成模式:" << std::endl;
        std::cout << "   ✓ -oe  空壳模块生成" << std::endl;
        std::cout << "   ✓ -oes 空壳+stub生成" << std::endl;
        std::cout << "   ✓ -oa  完整代码生成" << std::endl;
        std::cout << "   ✓ -oi  实例代码生成" << std::endl;
        std::cout << std::endl;
        
        if (failed == 0) {
            std::cout << "🎉 恭喜! 所有测试套件都通过了!" << std::endl;
            std::cout << "   Moparse工具已经准备好用于生产环境。" << std::endl;
        } else {
            std::cout << "⚠️  有 " << failed << " 个测试套件失败。" << std::endl;
            std::cout << "   请检查失败的测试并修复相关问题。" << std::endl;
        }
        
        std::cout << std::endl;
        std::cout << "==================================================" << std::endl;
    }
    
    std::vector<TestSuite> test_suites_;
};

int main() {
    ComprehensiveTestRunner runner;
    return runner.run_all_tests();
}